<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of v_txalign</title>
  <meta name="keywords" content="v_txalign">
  <meta name="description" content="V_TXALIGN Find the best alignment of two sets of time markers [KX,KY,N,M,S]=(X,Y,MAXT)">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">v_mfiles</a> &gt; v_txalign.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for v_mfiles&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>v_txalign
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>V_TXALIGN Find the best alignment of two sets of time markers [KX,KY,N,M,S]=(X,Y,MAXT)</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment">V_TXALIGN Find the best alignment of two sets of time markers [KX,KY,N,M,S]=(X,Y,MAXT)
 x and y vectors contain a list of non-decreasing time values
 we find the alignment between them to minimize (x-y)^2 with a penalty of maxt^2
 for every unmatched pair of entries from x and y.
 an x value can only match to the nearest y value in either the +ve or -ve direction
 If nsd is specified then the threshold is defined circularly to be nsd 
 times the std deviation of the matches away from the mean</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="v_txalign.html" class="code" title="function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd)">v_txalign</a>	V_TXALIGN Find the best alignment of two sets of time markers [KX,KY,N,M,S]=(X,Y,MAXT)</li></ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="v_txalign.html" class="code" title="function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd)">v_txalign</a>	V_TXALIGN Find the best alignment of two sets of time markers [KX,KY,N,M,S]=(X,Y,MAXT)</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd)</a>
0002 <span class="comment">%V_TXALIGN Find the best alignment of two sets of time markers [KX,KY,N,M,S]=(X,Y,MAXT)</span>
0003 <span class="comment">% x and y vectors contain a list of non-decreasing time values</span>
0004 <span class="comment">% we find the alignment between them to minimize (x-y)^2 with a penalty of maxt^2</span>
0005 <span class="comment">% for every unmatched pair of entries from x and y.</span>
0006 <span class="comment">% an x value can only match to the nearest y value in either the +ve or -ve direction</span>
0007 <span class="comment">% If nsd is specified then the threshold is defined circularly to be nsd</span>
0008 <span class="comment">% times the std deviation of the matches away from the mean</span>
0009 
0010 <span class="comment">% e.g. v_txalign([1 11 21 27 31 42 51],[2 12 15 22 25 32 41 52 61],1.1);</span>
0011 
0012 <span class="comment">%</span>
0013 <span class="comment">% Outputs: kx is a column vector the same length as x. kx(i)=j if</span>
0014 <span class="comment">%             x(i) is matched to y(j). kx(i)=0 if x(i) is unmatched.</span>
0015 <span class="comment">%          ky is a column vector the same length as y. ky(j)=i if</span>
0016 <span class="comment">%             y(j) is matched to x(i). kx(j)=0 if y(j) is unmatched.</span>
0017 <span class="comment">%          nxy is the number of matched pairs</span>
0018 <span class="comment">%          mxy is the mean of y(j)-x(i) for the matched pairs</span>
0019 <span class="comment">%          sxy is the standard deviation (biassed) of y(j)-x(i) for matched pairs</span>
0020 <span class="comment">%</span>
0021 <span class="comment">% If no outputs are specified, v_txalign plots a graph showing y-x against mean(x,y)</span>
0022 
0023 <span class="comment">%      Copyright (C) Mike Brookes 2002</span>
0024 <span class="comment">%      Version: $Id: v_txalign.m 10865 2018-09-21 17:22:45Z dmb $</span>
0025 <span class="comment">%</span>
0026 <span class="comment">%   VOICEBOX is a MATLAB toolbox for speech processing.</span>
0027 <span class="comment">%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html%</span>
0028 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0029 <span class="comment">%   This program is free software; you can redistribute it and/or modify</span>
0030 <span class="comment">%   it under the terms of the GNU General Public License as published by</span>
0031 <span class="comment">%   the Free Software Foundation; either version 2 of the License, or</span>
0032 <span class="comment">%   (at your option) any later version.</span>
0033 <span class="comment">%</span>
0034 <span class="comment">%   This program is distributed in the hope that it will be useful,</span>
0035 <span class="comment">%   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
0036 <span class="comment">%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
0037 <span class="comment">%   GNU General Public License for more details.</span>
0038 <span class="comment">%</span>
0039 <span class="comment">%   You can obtain a copy of the GNU General Public License from</span>
0040 <span class="comment">%   http://www.gnu.org/copyleft/gpl.html or by writing to</span>
0041 <span class="comment">%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.</span>
0042 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0043 
0044 <span class="comment">% d(*,i): i=1 cumulative cost if not the second of a pair</span>
0045 <span class="comment">%           2 cumulative cost if the second of a pair</span>
0046 <span class="comment">%           3 +1 if second of pair given that next is not second of pair</span>
0047 <span class="comment">%             -1 if not allowed to be second of pair</span>
0048 <span class="comment">%           4 0 if this is an x value, 1 if a y value</span>
0049 lx=length(x);
0050 ly=length(y);
0051 
0052 <span class="keyword">if</span> nargin&lt;4 <span class="comment">% no nsd specified</span>
0053 x(lx+1)=2*abs(y(ly))+1;
0054 y(ly+1)=2*abs(x(lx))+1;
0055 lxy=lx+ly+1;
0056 d=zeros(lxy,4);
0057 
0058 <span class="keyword">if</span> lx&gt;0 &amp; ly&gt;0
0059 c0=maxt^2;
0060 ix=1;
0061 iy=1;
0062 d(1,:)=[0 0 -1 y(1)&lt;x(1)];
0063 vp=0;
0064 <span class="keyword">for</span> id=2:lxy
0065     <span class="keyword">if</span> y(iy)&lt;x(ix) <span class="comment">% do y next</span>
0066         v=y(iy);
0067         d(id,4)=1;
0068         iy=iy+1;
0069     <span class="keyword">else</span> <span class="comment">% do x next</span>
0070         v=x(ix);
0071         ix=ix+1;
0072     <span class="keyword">end</span>
0073     <span class="keyword">if</span> d(id,4)==d(id-1,4)
0074         d(id,3)=-1;
0075     <span class="keyword">else</span>
0076         d(id,2)=d(id-1,1)-c0+(v-vp)^2;
0077     <span class="keyword">end</span>
0078     <span class="keyword">if</span> ~d(id-1,3) &amp; d(id-1,1)&gt;=d(id-1,2)
0079         d(id,1)=d(id-1,2);
0080         d(id-1,3)=1;
0081     <span class="keyword">else</span>
0082         d(id,1)=d(id-1,1);
0083     <span class="keyword">end</span>
0084     vp=v;
0085 <span class="keyword">end</span>
0086 <span class="keyword">if</span> ~d(lxy,3) &amp; d(lxy,1)&gt;=d(lxy,2)
0087     d(lxy,3)=1;
0088 <span class="keyword">end</span>
0089 
0090 <span class="comment">% now do the traceback</span>
0091 
0092 ix=lx;
0093 iy=ly;
0094 nxy=0;
0095 mxy=0;
0096 sxy=0;
0097 kx=zeros(lx,1);
0098 ky=zeros(ly,1);
0099 <span class="keyword">while</span> (ix&gt;0) &amp; (iy&gt;0)
0100     id=ix+iy+1;
0101     <span class="keyword">if</span> d(id,3)&gt;0
0102         ky(iy)=ix;  <span class="comment">% iy aligned with ix</span>
0103         kx(ix)=iy;
0104         dt=y(iy)-x(ix);
0105         nxy=nxy+1;
0106         mxy=mxy+dt;
0107         sxy=sxy+dt^2;
0108         ix=ix-1;
0109         iy=iy-1;
0110     <span class="keyword">else</span>
0111         ix = ix-1+d(id,4);
0112         iy = iy-d(id,4);
0113     <span class="keyword">end</span>
0114 <span class="keyword">end</span>
0115 <span class="keyword">if</span> nxy
0116     mxy=mxy/nxy;
0117     sxy=sqrt(sxy/nxy-mxy^2);
0118 <span class="keyword">end</span>
0119 <span class="comment">% eliminate junk and plot results</span>
0120 <span class="keyword">if</span> ~nargout
0121     x(end)=[];
0122     y(end)=[];
0123     x=x(:);
0124     y=y(:);
0125     p=zeros(lxy-nxy-1,3);
0126     p=[x x -(kx==0); y(ky==0) y(ky==0) ones(ly-nxy,1)];
0127     p(kx&gt;0,2)=y(kx(kx&gt;0));
0128     p=sortrows(p);
0129     p(:,1:2)=p(:,1:2)*[0.5 -1; 0.5 1];
0130     plot(p(p(:,3)==0,1),p(p(:,3)==0,2),<span class="string">'-*'</span>,p(p(:,3)&lt;0,1),p(p(:,3)&lt;0,2),<span class="string">'x'</span>,p(p(:,3)&gt;0,1),p(p(:,3)&gt;0,2),<span class="string">'o'</span>);
0131     xlabel(<span class="string">'Mean of x and y (x,o = unmatched x,y)'</span>);
0132     ylabel(<span class="string">'y-x difference'</span>);
0133 <span class="keyword">end</span>
0134 <span class="keyword">else</span> <span class="comment">% nsd specifies how many std deviations to exclude</span>
0135     [kx,ky,nxy,mxy,sxy]=<a href="v_txalign.html" class="code" title="function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd)">v_txalign</a>(x,y,maxt);
0136     nxy0=nxy+1;
0137     <span class="keyword">while</span> nxy&lt;nxy0
0138         nxy0=nxy;
0139         mxy0=mxy;
0140         [kx,ky,nxy,mxy,sxy]=<a href="v_txalign.html" class="code" title="function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd)">v_txalign</a>(x+mxy0,y,nsd*sxy);
0141     <span class="keyword">end</span>
0142     mxy=mxy+mxy0;
0143     <span class="keyword">if</span> ~nargout
0144         <a href="v_txalign.html" class="code" title="function [kx,ky,nxy,mxy,sxy]=v_txalign(x,y,maxt,nsd)">v_txalign</a>(x,y,nsd*sxy); <span class="comment">% plot it</span>
0145     <span class="keyword">end</span>
0146 <span class="keyword">end</span>
0147 <span class="keyword">else</span>
0148     kx=zeros(lx,1);
0149     ky=zeros(ly,1);
0150     nxy=0;
0151     mxy=0;
0152     sxy=0;
0153 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>